package com.front.managementfront.driver.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.alibaba.fastjson.JSON;
import com.front.managementfront.driver.entity.CarInfo;
import com.front.managementfront.driver.service.ICarInfoService;
import com.front.managementfront.driver.vo.CarInfoListVo;
import com.front.managementfront.fegin.IUserAppServiceClient;
import com.front.managementfront.utils.CustomPageUtils;
import com.hkts.management.common.utils.Result;
import com.hkts.management.common.utils.ResultUtil;
import com.hkts.management.common.vo.UserBasicInfoVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author yck
 * @since 2021-11-19
 */
@RestController
@RequestMapping("/front/carInfo")
public class CarInfoController {
    private final Logger logger = LoggerFactory.getLogger(CarInfoController.class);

    @Autowired
    public ICarInfoService iCarInfoService;
    @Autowired
    private IUserAppServiceClient iUserAppServiceClient;

    /**
     * 分页查询数据
     *
     * @param page    分页信息
     * @param carInfo 查询条件
     * @return
     */
    @ResponseBody
    @GetMapping("/getCarInfoPageList")
    public Result<Object> getHontUserList(Page<CarInfo> page, CarInfo carInfo) {
        return ResultUtil.data(CustomPageUtils.getCustomPage(iCarInfoService.selectPage(page, carInfo)));
    }

    /**
     * 保存和修改公用的
     *
     * @param carInfo 传递的实体
     * @return saveOrUpdate
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/carInfoSave")
    public Result<Object> carInfoSave(CarInfo carInfo) {
        iCarInfoService.saveOrUpdate(carInfo);
        return ResultUtil.success("保存成功");
    }

    /**
     * 根据id获取对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/carInfoGetById")
    public Result<Object> carInfoGetById(String id) {
        CarInfo byId = iCarInfoService.getById(id);
        return ResultUtil.data(byId);
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/carInfoDelete")
    public Result<Object> hontUserDelete(String id) {
        iCarInfoService.removeById(id);
        return ResultUtil.success("删除成功");
    }

    /**
     * 批量删除对象
     *
     * @param item 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/carInfoBatchDelete")
    public Result<Object> deleteBatchIds(String item) {
        List<String> ids = (List<String>) JSON.parse(item);
        iCarInfoService.removeByIds(ids);
        return ResultUtil.success("删除成功");
    }
    
    /** 功能描述:
    *根据用户id 查询已经审核车辆列表和未审核车辆列表
    * @return: 
    * @Author: 杨春科
    * @Date: 2021-11-19 19:37
    * @Description //TODO 
    */
    @ResponseBody
    @GetMapping("/getCarInfoByUserId")
    public Result<Object> getCarInfoByUserId(String userId) {
        CarInfoListVo vo=iCarInfoService.getCarInfoByUserId(userId);
        return ResultUtil.data(vo);
    }

    @ResponseBody
    @GetMapping("/getCarInfoByUserIdAndStatus")
    public Result<List<CarInfo>> getCarInfoByUserIdAndStatus(String userId,Integer approvalStatus ) {
        List<CarInfo> vo=iCarInfoService.getCarInfoByUserIdAndStatus(userId,approvalStatus);
        return ResultUtil.data(vo);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/updateCarInfoStatus")
    public Result<Object> updateCarInfoStatus(String id,Integer approvalStatus) {
        CarInfo byId = iCarInfoService.getById(id);
        if(byId.getApprovalStatus().equals(approvalStatus)){
            return ResultUtil.error("状态已经修改，请勿重复点击！");
        }
        byId.setApprovalType(1);
        byId.setApprovalStatus(approvalStatus);
        Result<UserBasicInfoVo> userBasicInfoVoResult = iUserAppServiceClient.remoteUserBasicInfo();
        byId.setApprovalUser(userBasicInfoVoResult.getResult().getId());
        byId.setApprovalTime(LocalDateTime.now());
        iCarInfoService.saveOrUpdate(byId);
        return ResultUtil.success("修改成功！");
    }

    @ResponseBody
    @GetMapping("/getCarInfoListByUserId")
    public Result<List<CarInfo>> getCarInfoListByUserId(String userId) {
        List<CarInfo> vo=iCarInfoService.getCarInfoListByUserId(userId);
        return ResultUtil.data(vo);
    }
}
